package com.zjitc.chapter13.controller;

import com.zjitc.chapter13.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @RequestMapping("/index")
    public String index() {
        System.out.println("UserController.index");
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        System.out.println("UserController.login");
        return "login";
    }

    @RequestMapping("/orderInfo")
    public String orderInfo() {
        System.out.println("UserController.orderInfo");
        return "orderInfo";
    }

    @RequestMapping("/login")
    public String login(User user, Model model, HttpSession session) {
        System.out.println("UserController.login");
        if ("admin".equals(user.getUsername()) && "123".equals(user.getPassword())) {
            session.setAttribute("user_session", user);
            return "index";
        } else {
            model.addAttribute("msg", "用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        System.out.println("UserController.logout");
        session.invalidate();
        return "index";

    }
}
